package com.sdkj.seckill.rocketmqTest.delayMessage;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

/**
 * @Description 发送延时消息
 * @Author Jiang
 * @Date 2022/01/18 20:57
 * @Version 1.0.0
 */

public class ScheduledMessageProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
        producer.setNamesrvAddr("47.94.172.125:9876");
        producer.start();
        int totalMessagesToSend = 100;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message("TestTopic",("Hello scheduled message "+i).getBytes());
            //设置延时等级3，这个消息将在10s后发送(现在只支持固定的几个时间，详看delayTimeLevel)
            message.setDelayTimeLevel(3);
            producer.send(message);
        }
        //关闭
        producer.shutdown();
    }
}
